package com.mine.crawler.util;

import java.sql.Connection;
import java.sql.DriverManager;

import org.apache.log4j.Logger;

import com.mine.crawler.config.InitializeCrawlerApplication;

public class MySqlDataBase
{
	private static Logger log = Logger.getLogger(MySqlDataBase.class);
	private static String MySQLDB_UserName;
	private static String MySQLDB_UserPasswd;
	private static String MySQLDB_DBName;
	private static String prefix_DbUrl = "jdbc:mysql://localhost/";
	
	public static void test()
	{
		Connection conn = null;
		MySQLDB_UserName = InitializeCrawlerApplication.
					MySqlDBConfig.getProperty("mycrawlerz.mysql.username");
		MySQLDB_UserPasswd = InitializeCrawlerApplication.
					MySqlDBConfig.getProperty("mycrawlerz.mysql.password");
		MySQLDB_DBName = InitializeCrawlerApplication.
					MySqlDBConfig.getProperty("mycrawlerz.mysql.dbName");
		try
		{
			String url = prefix_DbUrl+MySQLDB_DBName;
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			conn = DriverManager.getConnection(url, 
					MySQLDB_UserName, MySQLDB_UserPasswd);
			log.info("Database connection established");
		}
		catch (Exception e)
		{
			log.error("Cannot connect to database server"+e);
		}
		finally
		{
			if (conn != null)
			{
				try
				{
					conn.close();
					log.info("Database connection terminated");
				}
				catch (Exception e)
				{ /* ignore close errors */
				}
			}
		}
	}

}
